package com.boss.fileapp.dao.impl;

import com.boss.fileapp.consts.TableName;
import com.boss.fileapp.dao.IFilePathConfigDao;
import com.boss.fileapp.entity.FilePathConfig;
import com.boss.framework.dao.BaseDaoImpl;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Repository
public class FilePathConfigDaoImpl extends BaseDaoImpl implements IFilePathConfigDao {

    @Override
    public List<FilePathConfig> selectFilePathConfigByProvince(String province){
        StringBuilder sb = new StringBuilder();
        List<Object> paramList = new ArrayList<>();
        sb.append(" select ").append(" bank_code, upload_dir_path, download_dir_path, mof_div_code, bank_name ")
                .append(" from ").append(TableName.FILE_PATH_CONFIG)
                .append(" where ").append(" 1=1 ");
        if(!StringUtils.isEmpty(province)){
            sb.append(" and mof_div_code = ? ");
            paramList.add(province);
        }
        return super.getjdbcTemplate().query(sb.toString(), paramList.toArray(), new BeanPropertyRowMapper<>(FilePathConfig.class));
    }

    @Override
    public String selectUploadDirByDownloadDir(String downloadDir) {
        StringBuilder sb = new StringBuilder();
        sb.append(" select upload_dir_path from ").append(TableName.FILE_PATH_CONFIG)
                .append(" where ").append(" download_dir_path = ? ");
        return super.getjdbcTemplate().queryForObject(sb.toString(), String.class, downloadDir);
    }

    @Override
    public List<FilePathConfig> selectBankCodeAndUploadDir(String province,String sendBankCode) {
        List<String> params=new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        sb.append(" select ").append(" bank_code bankCode, upload_dir_path uploadDirPath ")
                .append(" from ").append(TableName.FILE_PATH_CONFIG)
                .append(" where ").append(" mof_div_code = ? ");
        params.add(province);
        if(!StringUtils.isEmpty(sendBankCode)){
            sb.append(" and bank_code=? ");
            params.add(sendBankCode);
        }
        return super.getjdbcTemplate().query(sb.toString(), new BeanPropertyRowMapper<>(FilePathConfig.class), params.toArray());
    }
}
